package com.ymatou.iapi.productquery.testcase;

import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productquery.parameter.GetProductBriefByProductIdListBean;
import com.ymatou.iapi.productquery.parameter.ProductBatchBriefDto;
import com.ymatou.iapi.productquery.service.GetProductBriefByProductIdListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.mongo.jproduct.CatalogsWapper;
import com.ymttest.database.mongo.jproduct.ProductsWapper;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import org.json.JSONArray;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.util.*;
import java.util.stream.Collectors;

/**
*批量查询商品简要信息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetProductBriefByProductIdList{
	private static GetProductBriefByProductIdListBean getproductbriefbyproductidlistBean;
	private static GetProductBriefByProductIdListCall getproductbriefbyproductidlistCall;
	private static Integer sellerId = Integer.valueOf(EnvSetup.getData("productpriceSellerId"));
	private static ProductsWapper productsWapper = new ProductsWapper();
	private static CatalogsWapper catalogsWapper = new CatalogsWapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量查询商品简要信息");
	}

	@Before
	public void caseUp() {
		getproductbriefbyproductidlistBean=new GetProductBriefByProductIdListBean();
		getproductbriefbyproductidlistCall=new GetProductBriefByProductIdListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getproductbriefbyproductidlistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void verify(GetProductBriefByProductIdListBean getproductBean){
		List<ProductBatchBriefDto> productList = getproductbriefbyproductidlistCall.getProductBatchBriefDtoList();
		System.out.println("--------"+productList);
		productList.forEach(p->{
			getproductBean.getProductIdList().forEach(x->{
				if (p.getProductId().equals(x)){
					try {
						Map<String, Object> productMap = productsWapper.getByProductId(x);
						List<Map> allByProductId = catalogsWapper.getAllByProductId(x);
						System.out.println(productMap);
						System.out.println(allByProductId);
						Logger.verifyEquals(p.getSellerId(),Integer.parseInt(productMap.get("sid").toString()),"验证sellerId");
						Logger.verifyEquals(p.getBrandId(),Integer.parseInt(productMap.get("bid").toString()),"验证BrandId");
						Logger.verifyEquals(p.getBrandEnName(),productMap.get("ebrand").toString(),"验证BrandEnName");
						Logger.verifyEquals(p.getBrandName(),productMap.get("brand").toString(),"验证brandName");
						Logger.verifyEquals(p.getThirdCategoryId(),Integer.parseInt(productMap.get("tcatid").toString()),"验证ThirdCategoryId");
						Logger.verifyEquals(p.getThirdCategoryName(),productMap.get("tcatname").toString(),"验证ThirdCategoryName");
						Logger.verifyEquals(p.getTitle(),productMap.get("title").toString(),"验证title");
						JSONArray jsonArray = new JSONArray(productMap.get("pics").toString());
						Logger.verifyEquals(p.getMainPic(),jsonArray.getString(0),"验证MainPic");
						List<Double> price = allByProductId.stream().map(o -> Double.parseDouble(o.get("price").toString())).collect(Collectors.toList());
						Double min = Collections.min(price);
						Logger.verifyEquals(p.getMinPrice(),min,"验证minPrice");
						
						Logger.verifyEquals(p.getMasterCategoryId(),productMap.get("mcatid").toString(),"验证masterCategoryId");
						Logger.verifyEquals(p.getCategoryId(),productMap.get("scatid").toString(),"验证cateGoryId");
						
						Logger.verifyEquals(p.isIsPspProduct()+"",productMap.get("ispsp").toString(),"验证isPspProduct");
						Logger.verifyEquals(p.getProductType()+"",productMap.get("ptype").toString(),"验证productType");
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				
				
			});
		});
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductBriefByProductIdList_01() {
		Logger.start(true,"批量查询商品简要信息");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			Thread.sleep(2000);
			getproductbriefbyproductidlistBean.setProductIdList(Arrays.asList(productId));
			getproductbriefbyproductidlistCall.setData(getproductbriefbyproductidlistBean);
			getproductbriefbyproductidlistCall.callService();
			Logger.verifyEquals("200",getproductbriefbyproductidlistCall.getString("Code"),"验证code");
			verify(getproductbriefbyproductidlistBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductBriefByProductIdList_02() {
		Logger.start(true,"批量查询商品简要信息,商品id数量>50");
		try {
			List<String> productIds = new ArrayList<>();
			for (int i =0;i<51;i++){
				AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
				String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
				productIds.add(productId);
			}

			Thread.sleep(2000);
			getproductbriefbyproductidlistBean.setProductIdList(productIds);
			getproductbriefbyproductidlistCall.setData(getproductbriefbyproductidlistBean);
			getproductbriefbyproductidlistCall.callService();
			Logger.verifyEquals("201",getproductbriefbyproductidlistCall.getString("Code"),"验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductBriefByProductIdList_03() {
		Logger.start(true,"批量查询商品简要信息,商品id数量为0");
		try {
			getproductbriefbyproductidlistBean.setProductIdList(null);
			getproductbriefbyproductidlistCall.setData(getproductbriefbyproductidlistBean);
			getproductbriefbyproductidlistCall.callService();
			Logger.verifyEquals("201",getproductbriefbyproductidlistCall.getString("Code"),"验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductBriefByProductIdList_04() {
		Logger.start(true,"批量查询商品简要信息--交易隔离");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			addpro.getProduct().getCatalogList().remove(0);
			// 设置商品库存,规格库存
			Integer catalogNum = 1000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});
			// 获取商品ID，单规格
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			// 获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream()
					.map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			
			// 创建一个活动 得到活动id
			Integer activityId = ProductManagerCallServiceV2.createIsolationActivityInProcess();
			// 报名活动 获得商品在活动中的id
			int actNum = 50;
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId, catalogs, activityId,
					actNum, "10");
			// 审核通过
			ProductManagerCallServiceV2.approveProductInActivity(productId, aids.get(0), true);
			Thread.sleep(2000);
			
			getproductbriefbyproductidlistBean.setProductIdList(Arrays.asList(productId));
			getproductbriefbyproductidlistBean.setIsTradeIsolation(true);
			getproductbriefbyproductidlistCall.setData(getproductbriefbyproductidlistBean);
			getproductbriefbyproductidlistCall.callService();
			Logger.verifyEquals("200",getproductbriefbyproductidlistCall.getString("Code"),"验证code");
			verify(getproductbriefbyproductidlistBean);
			Logger.verifyEquals(activityId,getproductbriefbyproductidlistCall.getJsonObject().getAsJsonArray("Data").get(0).getAsJsonObject()
					.get("ActivityId").getAsInt(),"验证activityId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductBriefByProductIdList_05() {
		Logger.start(true,"批量查询商品简要信息--非交易隔离");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			addpro.getProduct().getCatalogList().remove(0);
			// 设置商品库存,规格库存
			Integer catalogNum = 1000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});
			// 获取商品ID，单规格
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			// 获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream()
					.map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			// 创建一个非交易隔离活动 得到活动id
			Integer activityId = ProductManagerCallServiceV2.createNoIsolationActivityInProcess();
			// 报名活动 获得商品在活动中的id
			int actNum = 50;
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId, catalogs, activityId,
					actNum, "10");
			// 审核通过
			ProductManagerCallServiceV2.approveProductInActivity(productId, aids.get(0), true);
			
			Thread.sleep(2000);
			getproductbriefbyproductidlistBean.setProductIdList(Arrays.asList(productId));
			getproductbriefbyproductidlistBean.setIsTradeIsolation(false);
			getproductbriefbyproductidlistCall.setData(getproductbriefbyproductidlistBean);
			getproductbriefbyproductidlistCall.callService();
			Logger.verifyEquals("200",getproductbriefbyproductidlistCall.getString("Code"),"验证code");
			verify(getproductbriefbyproductidlistBean);
			Logger.verifyEquals(activityId,getproductbriefbyproductidlistCall.getJsonObject().getAsJsonArray("Data").get(0).getAsJsonObject()
					.get("ActivityId").getAsInt(),"验证activityId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductBriefByProductIdList_06() {
		Logger.start(true,"批量查询商品简要信息--交易隔离活动，使用非交易隔离，查询不到");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			addpro.getProduct().getCatalogList().remove(0);
			// 设置商品库存,规格库存
			Integer catalogNum = 1000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});
			// 获取商品ID，单规格
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			// 获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream()
					.map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			// 创建一个活动 得到活动id
			Integer activityId = ProductManagerCallServiceV2.createIsolationActivityInProcess();
			// 报名活动 获得商品在活动中的id
			int actNum = 50;
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId, catalogs, activityId,
					actNum, "10");
			// 审核通过
			ProductManagerCallServiceV2.approveProductInActivity(productId, aids.get(0), true);
			
			Thread.sleep(2000);
			getproductbriefbyproductidlistBean.setProductIdList(Arrays.asList(productId));
			getproductbriefbyproductidlistBean.setIsTradeIsolation(false);
			getproductbriefbyproductidlistCall.setData(getproductbriefbyproductidlistBean);
			getproductbriefbyproductidlistCall.callService();
			Logger.verifyEquals("200",getproductbriefbyproductidlistCall.getString("Code"),"验证code");
			verify(getproductbriefbyproductidlistBean);
			Logger.verifyEquals(0,getproductbriefbyproductidlistCall.getJsonObject().getAsJsonArray("Data").get(0).getAsJsonObject()
					.get("ActivityId").getAsInt(),"验证activityId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductBriefByProductIdList_08() {
		Logger.start(true,"批量查询商品简要信息--非交易隔离活动，使用交易隔离，查询成功");
		//交易隔离可以查询出非交易隔离
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			addpro.getProduct().getCatalogList().remove(0);
			// 设置商品库存,规格库存
			Integer catalogNum = 1000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});
			// 获取商品ID，单规格
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			// 获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream()
					.map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			// 创建一个非交易隔离活动 得到活动id
			Integer activityId = ProductManagerCallServiceV2.createNoIsolationActivityInProcess();
			// 报名活动 获得商品在活动中的id
			int actNum = 50;
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId, catalogs, activityId,
					actNum, "10");
			// 审核通过
			ProductManagerCallServiceV2.approveProductInActivity(productId, aids.get(0), true);
			
			Thread.sleep(2000);
			getproductbriefbyproductidlistBean.setProductIdList(Arrays.asList(productId));
			getproductbriefbyproductidlistBean.setIsTradeIsolation(true);
			getproductbriefbyproductidlistCall.setData(getproductbriefbyproductidlistBean);
			getproductbriefbyproductidlistCall.callService();
			Logger.verifyEquals("200",getproductbriefbyproductidlistCall.getString("Code"),"验证code");
			verify(getproductbriefbyproductidlistBean);
			Logger.verifyEquals(activityId,getproductbriefbyproductidlistCall.getJsonObject().getAsJsonArray("Data").get(0).getAsJsonObject()
					.get("ActivityId").getAsInt(),"验证activityId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}